﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using System.Text;
using Gift.Model.ENUM;
using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.order
{
    public partial class OrderCancel : BaseAdmin
    {
        readonly DALOrder theOrder = new DALOrder();
        DB_Order oOrder;



  
        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sqlsort = GetSql();
            this.GridViewBind();
        }


        private string GetSql()
        {
            string sql = " 1=1 ";
            if (this.txtOrderCode.Text.Trim() != "")
            {
                sql = sql + " and T.ordercode like '%" + this.txtOrderCode.Text.Trim() + "%'";
            }
            return sql;
        }





        protected void Page_Load(object sender, EventArgs e)
        {

            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.OrderCancel);
                this.GridViewBind();
             
            }
        }

   

        protected void Grid1_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName == "deleted")
            {
                object[] keys = Grid1.DataKeys[e.RowIndex];

                string id = keys[0].ToString();
                 theOrder.CancelOrder(int.Parse(id));
                ExtAspNet.Alert.Show("订单取消成功！");
                this.GridViewBind();
    
            }
        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            GridViewBind();
        }


        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table = theOrder.GetListByPage(GetSql(), string.Format("{0} {1}", e.SortField, e.SortDirection), Grid1.PageIndex, Grid1.PageSize).Tables[0];
            Grid1.DataSource = table;
            Grid1.DataBind();
            ViewState["sort"] = e.SortField;
            ViewState["dir"] = e.SortDirection;
        }


        private void GridViewBind()
        {

            string sSql = GetSql();
            Grid1.RecordCount = theOrder.GetRecordCount(sSql);
            string strOrderBy = string.Format("{0} {1} "
                                               , ViewState["sort"] == null ? null : ViewState["sort"].ToString()
                                               , ViewState["dir"] == null ? null : ViewState["dir"].ToString());
            if (strOrderBy.Length < 13)
            {
                strOrderBy = "orderCode desc ";
            }
            DataTable table = theOrder.GetListByPage(sSql, strOrderBy, Grid1.PageIndex, Grid1.PageSize).Tables[0];
            Grid1.DataSource = table;
            Grid1.DataBind();
            Grid1.SelectedRowIndexArray = new int[] { };
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.GridViewBind();
        }


    }
}